package com.luck.pf.security.menu.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.luck.org.entity.SysMenu;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.action.BaseActionSupport;
import com.luck.pf.security.MyInvocationSecurityMetadataSource;
import com.luck.pf.security.menu.logic.IMenuLogic;
import com.luck.pf.security.menu.qry.MenuQry;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.ObjectUtil;

/**
 * 说明:
 * Copyright: Copyright (c) 2010
 * @author zhangwr 2011-8-11 上午09:40:33
 * @version 1.0
 */
public class MenuAction extends BaseActionSupport {
	private static final long serialVersionUID = 3309185508856481439L;
	IMenuLogic menuLogic;
	MenuQry qry;
	SysMenu entity;
	Long pId;
	List<SysMenu> menus;
	public String index() throws Exception{
		return "index";
	}
	public String tree() throws Exception{
		UserBean userBean = super.getUserInfo();
		if (StringUtils.isNotBlank(super.qryHex)) {
			qry = (MenuQry)ObjectUtil.getObjectFromHex(qryHex);
		}
		if (qry == null) {
			qry = new MenuQry();
		}
		qry.setUserInfo(userBean);
		menus = menuLogic.findAuthMenu(qry);
		super.title="菜单树";
		return "tree";
	}
	public String toAdd() throws Exception{
		//entity = new SysMenu();
		entity.setId(ADD_PK);
		super.title="增加菜单项";
		return "edit";
	}
	public String toEdit() throws Exception{
		entity = menuLogic.get(SysMenu.class, entity.getId());
		super.title="修改菜单项";
		//super.redirectUrl = "/security/menu_tree.action";
		return "edit";
	}
	public String edit() throws Exception{
		SysMenu parent = new SysMenu();
		UserBean userBean = super.getUserInfo();
		parent = menuLogic.get(SysMenu.class, entity.getParentId());
		entity.setOperTime(DateUtil.getDate());
		entity.setOperUser(userBean.getUserId());
		String str = "";
		try {
			if(entity.getId() == ADD_PK){//新增
				entity.setId(null);
				entity.setParentId(entity.getParentId());
				entity.setMenuLevel((new Long(parent.getMenuLevel())+1L)+"");
				entity = menuLogic.saveMenu(entity);
			}else{//修改
				SysMenu db = menuLogic.get(SysMenu.class, entity.getId());
				super.copyProperties(db, entity);
				entity = menuLogic.saveMenu(db);
			}	
		} catch (Exception e) {
			str = e.getMessage();
		}
		Map<String, String> jsonMap = new HashMap<String, String>();
		if(StringUtils.isNotBlank(str)){
			jsonMap.put("status", ERROR);
			jsonMap.put("message", str);
		}else{
			jsonMap.put("menuid", entity.getId().toString());
			jsonMap.put("status", SUCCESS);
			jsonMap.put("message", "操作成功");
			releaseMenu();
		}
		JSONObject jsonObject = JSONObject.fromObject(jsonMap);
		this.rendText(jsonObject.toString());
		return null;
	}
	public String delete() throws Exception{
		UserBean userBean = super.getUserInfo();
		entity = menuLogic.get(SysMenu.class, entity.getId());
		Long pId = entity.getParentId();
		String str="";
		try {
			str = menuLogic.deleteMenu(entity,userBean, createLog(userBean, "删除菜单["+entity.getMenuName()+"]", "2"));
		} catch (Exception e) {
			str = e.getMessage();
		}
		if(StringUtils.isBlank(str)){
			Map<String, String> jsonMap = new HashMap<String, String>();
			jsonMap.put("status", SUCCESS);
			jsonMap.put("menuid", pId.toString());
			jsonMap.put("message", "操作成功");
			JSONObject jsonObject = JSONObject.fromObject(jsonMap);
			this.rendText(jsonObject.toString());
			releaseMenu();
		}else
			super.renderJsonError(str+"不能删除！");
		return null;
	}
	private void releaseMenu() {
		MyInvocationSecurityMetadataSource source = new MyInvocationSecurityMetadataSource();
		source.setMenuLogic(menuLogic);
		source.loadResourceDefine();
	}
	public String view() throws Exception{
		entity = menuLogic.get(SysMenu.class, entity.getId());
		return "view";
	}
	public IMenuLogic getMenuLogic() {
		return menuLogic;
	}
	public void setMenuLogic(IMenuLogic menuLogic) {
		this.menuLogic = menuLogic;
	}
	public MenuQry getQry() {
		return qry;
	}
	public void setQry(MenuQry qry) {
		this.qry = qry;
	}

	public SysMenu getEntity() {
		return entity;
	}
	public void setEntity(SysMenu entity) {
		this.entity = entity;
	}
	public List<SysMenu> getMenus() {
		return menus;
	}
	public void setMenus(List<SysMenu> menus) {
		this.menus = menus;
	}
	public Long getpId() {
		return pId;
	}
	public void setpId(Long pId) {
		this.pId = pId;
	}

}
